package com.zz.examination.controller;

import com.lkx.util.ExcelUtil;
import com.zz.examination.core.Result;
import com.zz.examination.core.ResultGenerator;
import com.zz.examination.dto.QuestionAnswerDto;
import com.zz.examination.entity.Question;
import com.zz.examination.repository.QuestionRepository;
import com.zz.examination.utils.ZUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Controller
@Slf4j
@RequestMapping("/question")
public class QuestionController {

    @Resource
    private QuestionRepository questionRepository;

    @PostMapping("/file")
    public Result addQuestion(@RequestBody MultipartFile file) {
        if (file.isEmpty()) {
            return ResultGenerator.genFailResult("未上传文件");
        }
        String keyValue = "考题内容:quesContent,解析:analysis,答案内容:ansContents";
        try {
            List<QuestionAnswerDto> list = ExcelUtil.readXls(file.getBytes(), ExcelUtil.getMap(keyValue), "com.zz.examination.dto.QuestionAnswerDto");
            // 0.考题表中原有数据要保留
            long count = questionRepository.count();
            if (count > 0) {
                list = list.subList((int) count, list.size());
            }

            list.parallelStream().forEach(dto -> {
                // 答案内容list
                List<String> ansContentList = dto.getAnsContentList();
                Question question = Question.builder()
                        .id(ZUtils.getUUID())
                        .ansContentList(ansContentList)
                        .rightAnswerContent(ansContentList.get(0))
                        .build();
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
